<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>开关</title>
    <style>
        .switch-box{
            width: 300px;
            height: 150px;
            overflow: hidden;
            border-radius: 150px;
            box-sizing: border-box;
            border: 1px solid #eeeeee;
        }
        .switch-ball {
            border: 1px solid #eeeeee;
            box-sizing: border-box;
            display: inline-block;
            width: 150px;
            height: 150px;
            font-size: 50px;
            line-height: 150px;
            border-radius: 50%;
            background-color: white;
            position: relative;
            top: -1px;
            text-align: center;
            transition: left 0.5s;
        }
        .switch-ball:before{
            content: "ON";
            display: block;
            width: 225px;
            height: 150px;
            position: absolute;
            left: -150px;
            top: 0px;
            z-index: -1;
            background-color: aqua;
        }
        .switch-ball:after{
            content: "OFF";
            display: block;
            width: 225px;
            height: 150px;
            position: absolute;
            top: -5px;
            left: 100px;
            z-index: -1;
            background-color: white;
        }
    </style>
</head>
<body>
    <div id="app">
        <input type="checkbox"  v-model="onoff">
        <switch-btn v-model="onoff"></switch-btn>
    </div>
</body>
<script src="./vue.js"></script>
<script type="text/html" id="switch">
    <div class="switch-box" >
        <span class="switch-ball" :style="onoff"  @click="btnClick"></span>
    </div>
</script>
<script>
    Vue.component('switchBtn', {
        template: "#switch",
        props:["value"],
        methods:{
            btnClick(){
                this.$emit("input", !this.value);
            }
        },
        computed:{
            onoff(){
                if (this.value) {
                    return {
                        left:'150px'
                    }
                }else{
                    return {
                        left:'0px'
                    }
                }
            }
        },
    });

    new Vue({
        el: "#app",
        data: {
            onoff: true,
        },
    })
</script>
</html>